package com.report.word;

import com.deepoove.poi.render.compute.EnvModel;
import com.deepoove.poi.render.compute.RenderDataCompute;
import com.report.Tools;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;

import java.util.Map;

public class CustomRenderDataCompute implements RenderDataCompute {

    private final ExpressionParser parser;
    private final EvaluationContext context;

    @SuppressWarnings("unchecked")
    public CustomRenderDataCompute(EnvModel model) {
        this.parser = new SpelExpressionParser();
        this.context = Tools.createSpelContext((Map<String, Object>) model.getRoot());
    }

    @Override
    public Object compute(String el) {
        return parser.parseExpression(el).getValue(context);
    }

    @SuppressWarnings("unchecked")
    public Map<String, Object> getRootObject() {
        return (Map<String, Object>) this.context.getRootObject().getValue();
    }

    public EvaluationContext getEvaluationContext() {
        return this.context;
    }
}
